<?php

require_once("View/View/PageView.php");
require_once("Common/Model/commonThings.php");
require_once("MyWorkouts/Model/WorkoutModel.php");
require_once("MyWorkouts/View/MyWorkoutsView.php");
require_once("login/src/Model/LoginValidation.php");

class MyWorkoutsController
{
	/**
	*@var PageView
	**/
	private $pageView;
	/**
	*@var MyWorkoutsView
	**/
	private $myWorkoutsView;
	/**
	*@var MyWorkoutsModel
	**/
	private $myWorkoutsModel;
	/**
	*@var Loginvalidation
	**/
	private $loginvalidation;


	public function __construct()
	{
		$this->pageView = new PageView();
		$this->myWorkoutsModel = new WorkoutModel();
		$this->myWorkoutsView = new MyWorkoutsView();
		$this->loginvalidation = new LoginValidation();
	}

	/**
	*@return string
	**/
	public function getPage()
	{
		if($this->myWorkoutsView->isItAdded())
		{
			$message = $this->myWorkoutsModel->getErrorMessage();
			if($message == "")
			{
				$this->addNewWorkout();
			}
		}
		elseif($this->myWorkoutsView->isItRemove())
		{
			//TODO, kalla på en modell klass som får ID som inparameter, och kalla
			// på en klass i databasen som tar bort workout på ID.DeleteWorkout($workoutID)
			$workoutId = $this->myWorkoutsView->getIdToRemove();
			$this->myWorkoutsModel->ValidateWorkoutIdAndDelete($workoutId);
			header("Location:MyWorkouts.php");
		}

		$html = $this->myWorkoutsView->getMyWorkoutsPage();
		$html = $this->pageView->getHtmlPage("Framsteg - Träningsdagboken", "", $html);
		return $html;
	}


	private function addNewWorkout()
	{
		if(commonThings::checkIfPost()) //TODO, kolla den här i alla klasser istället.
		{
			$name = $this->myWorkoutsView->getNewWorkoutName();
			$description = $this->myWorkoutsView->getNewWorkoutDescription();
			if($this->myWorkoutsModel->validateWorkout($name, $description))
			{	
				$exercises = $this->myWorkoutsView->getChosenExercises();
				$this->myWorkoutsModel->CreateAndSaveWorkout($name, $description, $exercises);
			}
		}
	}

	/**
	*@return boolean
	**/
	public function areWeLoggedIn()
	{
		if(!$this->loginvalidation->weAreLoggedIn())
		{
			header("Location:index.php");
			return false;
		}
		return true;
	}

}